package com.wink.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.wink.admin.redis.RedisService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

/**
 * 短链接controller
 *
 * @author xw
 * @date 2020-11-30 10:44
 */
@Slf4j
@RestController
@AllArgsConstructor
public class UrlController {

    private final RedisService redisService;

    @GetMapping("/url/{key}")
    public void jump(@NotBlank(message = "url不能为空！") @PathVariable String key, HttpServletResponse response) throws IOException {
        String url = (String) redisService.get(key);
        if (StrUtil.isBlank(url)){
            response.sendRedirect("www.baidu.com");
        }else {
            // 重定向
            response.sendRedirect(url);
        }
    }
}
